package view.frontend.order;

import exceptionManager.ExceptionManager;
import java.awt.Font;
import java.awt.event.*;
import java.sql.SQLException;
import javax.swing.*;
import javax.swing.border.TitledBorder;
import model.CustomerSpecified;
import view.generaldesign.DesignLayout;

/**
 *
 * @author Sonny Sandberg
 */
public class OrderCustomer
{
    private JPanel 
            orderCustomerPanel,
            orderCustomerSearch,
            orderCustomerListPanelHolder;
    private OrderCustomerList orderCustomerList;
    private Order order;
    private JTextField tFieldNumber;

    public OrderCustomer(Order view)
    {
        this.order = view;
        
        orderCustomerPanel = new JPanel(null);
        orderCustomerPanel.setBounds(0, 0, 300, 460);
        orderCustomerPanel.setOpaque(false);
        
        orderCustomerSearch = new JPanel(null);
        orderCustomerSearch.setBounds(0, 0, 300, 80);
        orderCustomerSearch.setBorder(BorderFactory.createTitledBorder(DesignLayout.T_FIELD_RED, "Søg", TitledBorder.DEFAULT_JUSTIFICATION, TitledBorder.DEFAULT_POSITION, DesignLayout.STANDARD));
        orderCustomerSearch.setOpaque(false);
        
        JLabel lblPhone = new JLabel("Telefonnummer");
        lblPhone.setBounds(10, 25, 280, 18);
        lblPhone.setFont(DesignLayout.STANDARD.deriveFont(Font.BOLD));
        
        tFieldNumber = new JTextField();
        tFieldNumber.setBounds(10, 45, 280, 25);
        tFieldNumber.setBorder(DesignLayout.T_FIELD_RED);
        tFieldNumber.setBackground(DesignLayout.T_FIELD_BG_COLOR);
        tFieldNumber.setFont(DesignLayout.STANDARD);
        tFieldNumber.setHorizontalAlignment(SwingConstants.CENTER);
        tFieldNumber.addKeyListener(new KeyAdapter() 
        {
            @Override
            public void keyReleased(KeyEvent e)
            {
                JTextField tfield = (JTextField) e.getComponent();
                if (tfield.getText().length() == 8)
                {
                    doLookUp(tfield.getText());
                }
                else if (tfield.getText().length() > 8)
                {
                    tfield.setText(""+tfield.getText().subSequence(0, 8));
                }
            }
        });
        
        orderCustomerSearch.add(lblPhone);
        orderCustomerSearch.add(tFieldNumber);
        
        orderCustomerList = new OrderCustomerList(order);
        orderCustomerListPanelHolder = orderCustomerList.getPanel();
        orderCustomerListPanelHolder.setVisible(true);
        
        orderCustomerPanel.add(orderCustomerListPanelHolder);
        orderCustomerPanel.add(orderCustomerSearch);
        orderCustomerPanel.setVisible(false);
    }
    
    private void doLookUp(String number)
    {
        try
        {
            CustomerSpecified[] customerSpecified = model.handlers.CustomerHandler.getSpecifiedCustomersByNumber(number);
            orderCustomerList.buildFoundPanel(customerSpecified);
        } 
        catch (SQLException ex)
        {
            ExceptionManager.handleSQLException(ex);
        } 
        catch (ClassNotFoundException ex)
        {
            ExceptionManager.handleClassNotFoundException(ex);
        }
    }

    public String gettFieldNumber()
    {
        return tFieldNumber.getText();
    }

    public void settFieldNumber(String tFieldNumber)
    {
        this.tFieldNumber.setText(tFieldNumber);
    }
    
    public OrderCustomerList getOrderCustomerList()
    {
        return orderCustomerList;
    }

    public JPanel getPanel()
    {
        return orderCustomerPanel;
    }
}